﻿/*
 * Created by SharpDevelop.
 * User: Corrado Mio
 * Date: 14/06/2012
 * Time: 10:23
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Data.Common;

namespace CSharpExt.Data
{
    /// <summary>
    /// Description of MetadataResultSet.
    /// </summary>
    public class MetadataResultSet
    {
        #region Private Data
        
        private DataResultSet _rset;
        private DbDataReader _reader;
        private MetadataResultColumn _column;
        
        //private Dictionary<String, int> _ordinals = new Dictionary<String, int>();
        
        #endregion
        
        #region Constructor
        
        internal MetadataResultSet(DataResultSet rset)
        {
            _rset = rset;
            _reader = rset.InnerReader;
            _column = new MetadataResultColumn(this);
        }
        
        #endregion
        
        #region Properties
        
        public int ColumnCount
        {
            get { return _reader.FieldCount; }
        }
        
        public MetadataResultColumn this[int ordinal]
        {
            get 
            {
                _column.Ordinal = ordinal;
                return _column; 
            }
        }
        
        public MetadataResultColumn this[String name]
        {
            get { return this[GetOrdinal(name)]; }
        }
        
        #endregion
        
        #region Operations
        
        public int GetOrdinal(String name)
        {
            return ordinalOf(name);
        }
        
        public String GetName(int ordinal)
        {
            return _reader.GetName(ordinal);
        }
  
        // ------------------------------------------------------------------        
        
        /// <summary>
        /// Tipo di dato second il Database
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public String GetDataType(int ordinal)
        {
            return _reader.GetDataTypeName(ordinal);
        }
        
        /// <summary>
        /// Tipo di dato secondo .NET
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public Type GetNETType(int ordinal)
        {
            return _reader.GetFieldType(ordinal);
        }
        
        // ------------------------------------------------------------------        
        
        public String GetDataTypeName(String name)
        {
            return GetDataType(ordinalOf(name));
        }
        
        public Type GetDataType(String name)
        {
            return GetNETType(ordinalOf(name));
        }
        
        #endregion
        
        #region Implementation
        
        internal int ordinalOf(String name)
        {
            return _rset.ordinalOf(name);
        }
        
        #endregion
    }
    
    
    public class MetadataResultColumn
    {
        #region Private Data
        
        private MetadataResultSet _mdrs;
        private int _ordinal;
        
        #endregion
        
        #region Constructor
        
        internal MetadataResultColumn(MetadataResultSet mdrs)
        {
            _mdrs = mdrs;
        }
        
        #endregion
        
        #region Properties
        
        internal int Ordinal
        {
            set { _ordinal = value; }
        }
        
        public String Name
        {
            get { return _mdrs.GetName(_ordinal); }
        }
        
        public String DataType
        {
            get { return _mdrs.GetDataType(_ordinal); }
        }
        
        public Type NETType
        {
            get { return _mdrs.GetNETType(_ordinal); }
        }  
        
        #endregion
    }
}
